﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using jaysJourney_v01.Utils.Output;

namespace jaysJourney_v01.Common
{
    public class ActivePlayer
    {
        /**
         * MEMBER VARIABLES
         * */

        // From Parent

        private MessageManager msgManager;

        // Common

        private String username;
        private int money;
        private int xp;

        private int lvlsSinceLast;
        private int moneySinceLast;
        private int xpSinceLast;
        public int progress;
        private bool firstLogin = true;

        public int lvl = 1;

        private String version;

        private String[] unlockedBlock = { "Dissolving Block", "Deadly Block", "Invisible Block" };
        private String[] unlockedSetting = { "Wilderness" };
        private int levelcap = 5;

        /**
         * METHODS
         * */

        // Constructor

        public ActivePlayer(MessageManager msg)
        {
            this.msgManager = msg;
        }

        // set player

        public void setPlayer(String name, int money, int xp)
        {
            this.username = name;
            this.money = money;
            this.xp = xp;

            calcLevel();
        }

        // set player incl last

        public void setPlayer(String name, int money, int xp, int lvlLast, int moneyLast, int xpLast, String version)
        {
            setPlayer(name, money, xp);

            this.lvlsSinceLast = lvlLast;
            this.moneySinceLast = moneyLast;
            this.xpSinceLast = xpLast;
            this.version = version;
        }

        // calculate player lvl

        private void calcLevel()
        {
            int buffer = this.lvl;
            this.lvl = 1;
            float cap = 500;
            float xpbuffer = xp;
            while (lvl<levelcap&&xpbuffer > cap)
            {
                lvl++;
                xpbuffer -= cap;
                cap += 500;
            }
            /*
            float target = 200;
            float multi = 2.2f;
            float decr = 0.95f;
            float oldtarget = 0;
            while (this.xp > target * multi * decr)
            {
                oldtarget = target;
                this.lvl++;
                target *= multi * decr;
                if(multi > 1.2) multi *= decr;
            }
            */
            this.progress = (int)(xpbuffer / cap*100);
            if (lvl == levelcap) this.progress = 100;

            

            if (this.lvl > buffer && buffer != 0 && !firstLogin)
            {
                this.msgManager.playLevelUpAnimation();
                if (this.lvl<5)this.msgManager.addMessage("new Block unlocked: "+unlockedBlock[this.lvl-2], false);
                if (this.lvl == 5) this.msgManager.addMessage("new Setting unlocked: " + unlockedSetting[0], false);
            }
            if (firstLogin) firstLogin = false;
        }

        // print data

        public void printData()
        {
            Debug.WriteLine("cur player: " + this.username + ", " + this.money + ", " + this.xp);
        }

        // get username

        public String getUsername()
        {
            return this.username;
        }

        // get money

        public int getMoney()
        {
            return this.money;
        }

        // set money

        public void setMoney(int money)
        {
            this.money = money;
        }
        // get xp

        public int getXp()
        {
            return this.xp;
        }

        // get lvlsSinceLast

        public int getLvlsSinceLast()
        {
            return this.lvlsSinceLast;
        }

        // get moneySinceLast

        public int getMoneySinceLast()
        {
            return this.moneySinceLast;
        }

        // get xpSinceLast

        public int getXpSinceLast()
        {
            return this.xpSinceLast;
        }

        // getLevel

        public int getCurrentLevel()
        {
            return this.lvl;
        }

        // getversion

        public String getVersion()
        {
            return this.version;
        }

        // clear userdata

        public void clearData()
        {
            this.username = "";
            this.money = 0;
            this.xp = 0;
            this.version = "";
            this.firstLogin = true;
        }

        
    }
}
